/*============================================================================
  File:      DmhVector.h
 
  Summary:   STL vector class that uses the IDMMemoryAllocator interface
			 to allocate memory.

  Date:		 June 30, 2004
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.
 
  Copyright (C) 2003 Microsoft Corporation.  All rights reserved.
 
This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.
 
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
============================================================================*/

#pragma once
#pragma warning(disable: 4996)
#include <vector>
#include "DmhAllocator.h"


using namespace std;



template<class _T>
class dmh_vector : public vector<_T, dmh_allocator<_T> >
{
  public:

    dmh_vector(DMHALLOC& dmhalloc) : vector<_T, dmh_allocator<_T> >(dmh_allocator<_T> (dmhalloc))
	{
	}
};

// In order to have a vector-of-vectors, the no-argument resize() needs the DMHALLOC. 

template<class _T>
class dmh_vector<dmh_vector<_T> > : public vector<dmh_vector<_T>, dmh_allocator<dmh_vector<_T> > > 
{
  public:

	dmh_vector(DMHALLOC& dmhalloc) : vector<dmh_vector<_T> , dmh_allocator<dmh_vector<_T> > >(dmh_allocator<dmh_vector<_T> > (dmhalloc))
	{
	}

	void resize(size_type _Newsize)
	{	
		DMHALLOC& dmhalloc = ((dmh_allocator<dmh_vector<_T> >&) get_allocator())._dmhalloc;

		vector<dmh_vector<_T>, dmh_allocator<dmh_vector<_T> > >::resize(_Newsize, dmh_vector<_T>(dmhalloc));
	}
};




#define DEFINEV(_T)		typedef dmh_vector<_T> V##_T
#define DEFINEVP(_T)	typedef dmh_vector<_T*> VP##_T

// Special vector of bool:

typedef dmh_vector<bool> VB;

// Common vector types

typedef int INT;
DEFINEV(INT);

typedef double DBL;
DEFINEV(DBL);

// STL will throw an exception if there's not enough memory to 
// perform an operation. We do not want to throw exceptions in
// the plug-in algorithm, so we'll use the following scheme to
// handle this: for each call to a vector resize or push_back,
// we'll wrap with the CHECK_STL_MEM macro below. Note that this
// requires that the current function returns an HRESULT and that
// no cleanup is required if we run out of memory... you should
// modify as appropriate if this is not appropriate for your
// algorithm.

#define CHECK_STL_MEM(expr)		\
{								\
	try							\
	{							\
		expr;					\
	}							\
	catch (...)					\
	{							\
		return E_OUTOFMEMORY;	\
	}							\
}		
